// pages/mtest/guardCheck/temperatureCheck/temperatureCheck.js

const app = getApp()
const networkUtil = app.globalData.networkUtil
const networkUtilV2 = app.globalData.networkUtilV2
const util = app.globalData.util

const logPrefix = "guardCheck.temperatureCheck"

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		factoryAreaId: null,
		inOutFlag: null,
		idcardno: null,
		temperature: null,
		companyname: '',
		topdept: '',
		deptname: '',
		employeename: '',
		isFirst: '', //首次进入厂区提示 (0: 不是首次; 1 是首次;)

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		console.log(logPrefix+" onLoad().options", options)
		//存入factoryAreaId,inOutFlag
		let that = this
		that.setData({
			factoryAreaId: options.factoryAreaId,
			inOutFlag: options.inOutFlag
		})
	},

	/**
	 * 失去焦点事件的处理。
	 * @param {*} event 
	 */
	onBlur: function (event) {
		let that = this
		let name = event.currentTarget.dataset.name
		let value = event.detail.value

		that.setData({
			[name]: value
		})
	},

	/**
	 * 扫描二维码，二维码由身份证号转换而成。
	 */
	scanQrcode_employee: function () {
		let that = this

		wx.scanCode({
			onlyFromCamera: true,
			success(res) {
				console.info(logPrefix + ".scanQrcode_employee()", res)
				that.setData({
					idcardno: res.result
				})

				let url = app.globalData.baseUrl + "/employeeInoutRecord/getEmployeeInfo"
				let requestData = {
					idcardno: res.result,
				}

				networkUtil.request_get(url, requestData).then((res) => {
					//获取信息成功
					that.setData({
						companyname: res.data.companyname,
						topdept: res.data.firstdeptname,
						deptname: res.data.deptname,
						employeename: res.data.employeename,
						isFirst: res.data.isFirst,
					})

				}).catch((res) => {
					//没有查到信息
					console.error(logPrefix + " 获取员工信息出错!", res)
					util.showToastWithMsg("获取员工信息出错!", "")
				})

			},
			fail(res) {
				console.error(logPrefix + ".scanQrcode_employee(),扫描二维码出错!", res)
				util.showToast("扫描二维码出错!")
			}
		})
	},

	/**
	 * 检查输入的体温。
	 * @param {*} event 
	 */
	checkTemperature: function (event) {
		let that = this
		let temperature = event.detail.value

		//是否为数字
		temperature = Number(temperature)
		if (isNaN(temperature)) {
			util.showToast("体温格式错误!")
			return
		}

		//给体温加上小数点
		if (temperature >= 100 && temperature < 1000) {
			temperature = temperature / 10
		}
		if (temperature >= 1000) {
			temperature = temperature / 100
		}

		//分析体温值是否在合理范围内
		if (temperature < 35 || temperature > 42) {
			util.showToast("体温不在合理范围内[35,42]")
			return
		}

		//存入体温值
		that.setData({
			temperature: temperature
		})
	},

	/**
	 * 提交体温检查信息。
	 */
	submitTemperatureCheck: function () {
		//验证信息是否完整
		let that = this
		let isValid = that.validateInfo()
		if (isValid == false) {
			return
		}

		//提交表单信息
		let url = app.globalData.baseUrl + "/employeeInoutRecord/addEmployeeInoutRecord"
		let requestData = {
			factoryContrastId: that.data.factoryAreaId,
			inoutFlag: that.data.inOutFlag,
			idcardno: that.data.idcardno,
			temperature: that.data.temperature,
		}

		networkUtilV2.httpPost(url, requestData).then((res) => {
			/**
			 * 若提交信息成功
			 */
			console.info(logPrefix + " 提交体温检测信息成功!", res)
			util.showToast("提交成功!")
			/**
			 * 清空部分字段。
			 */
			that.setData({
				idcardno: null,
				temperature: null,
				companyname: '',
				topdept: '',
				deptname: '',
				employeename: '',
				employeeno: '',
				isFirst: ''
			})

		}).catch((res) => {
			/**
			 * 若提交信息失败，打印失败信息。
			 */
			console.error(logPrefix + " 提交体温检测信息出错!", res)
			util.showToastWithMsg("提交体温检测信息出错!", res.message)
		})
	},

	/**
	 * 验证信息是否完整,若不完整,返回false; 若完整返回true。
	 */
	validateInfo: function () {
		let that = this

		if (that.data.factoryAreaId == "null" || util.isEmpty(that.data.factoryAreaId)) {
			util.showToast("请选择厂区")
			return false
		}
		if (that.data.inOutFlag == "null" || util.isEmpty(that.data.inOutFlag)) {
			util.showToast("请选择进门或出门")
			return false
		}

		//身份证号
		if (util.isEmpty(that.data.idcardno)) {
			util.showToast("请输入身份证号")
			return false
		}

		//若进门,输入体温
		if (that.data.inOutFlag == 1) {
			if (util.isEmpty(that.data.temperature)) {
				util.showToast("请输入体温")
				return false
			}
		}

		//若表单完整
		return true
	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})